package com.example.springbootrbac.controller;

import com.example.springbootrbac.entity.*;
import com.example.springbootrbac.pojo.Result;
import com.example.springbootrbac.pojo.ResultCode;
import com.example.springbootrbac.service.InsertService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
public class InsertController {
    @Resource(name = "insertServiceImpl")
    InsertService insertService;

    @PostMapping("/index/messageBoards/insert")
    public Result<String> insertMessageBoard(MessagesBoardEntity messagesBoard, HttpServletRequest request) {
        System.out.println("前端参数:id=" + messagesBoard.getText());
        if (ObjectUtils.isEmpty(messagesBoard.getText())) {
            return Result.failure(ResultCode.FAILURE_INSERT_ERROR, "留言可不能留空白哟");
        }
        String token = insertService.insertMessageBoard(messagesBoard, request);
        if (!ObjectUtils.isEmpty(token)) {
            return Result.success("发布成功");
        }
        return Result.failure(ResultCode.FAILURE_INSERT_ERROR, "留言不能超过50个字符哦~");
    }

    @PostMapping("/index/course/insert")
    public Result<String> insertCourse(CourseEntity course, HttpServletRequest request) {
        System.out.println("前端参数:id=" + course);
        if (ObjectUtils.isEmpty(course.getName()) || ObjectUtils.isEmpty(course.getTeacher()) || ObjectUtils.isEmpty(course.getAddress())) {
            return Result.failure(ResultCode.FAILURE_INSERT_ERROR, "这三项必填哦");
        }
        String token = insertService.insertCourse(course, request);

        if (ObjectUtils.isEmpty(token)){
            return Result.failure(ResultCode.FAILURE_INSERT_ERROR, "相同的课程已存在");
        }else if("FK_ERROR_1".equals(token)){
            return Result.failure(ResultCode.FAILURE_UPDATE_FOREIGN_KEY_ERROR,"不存在此教室");
        }else if("FK_ERROR_2".equals(token)){
            return  Result.failure(ResultCode.FAILURE_UPDATE_FOREIGN_KEY_ERROR,"不存在此任课教师");
        }
       return  Result.success("已成功添加课程！");
    }

    @PostMapping("/index/room/insert")
    public Result<String> insertRoom(RoomEntity room, String buildingName, HttpServletRequest request) {
        System.out.println("前端参数:id=" + room + "所属楼宇：" + buildingName);
        if (ObjectUtils.isEmpty(room.getName()) || ObjectUtils.isEmpty(buildingName) || ObjectUtils.isEmpty(room.getHousehold())) {
            return Result.failure(ResultCode.FAILURE_INSERT_ERROR, "房间号，户主和所在楼宇不能为空哦");
        }
        String token = insertService.insertRoom(room, buildingName, request);
        if (!ObjectUtils.isEmpty(token)) {
            return Result.success("添加成功");
        }
        return Result.failure(ResultCode.FAILURE_INSERT_ERROR, "所属楼宇不存在或房间已经存在");
    }

    @PostMapping("/index/announcement/insert")
    public Result<String> insertAnnouncement(AnnouncementEntity announcement, HttpServletRequest request) {
        System.out.println("前端参数:id=" + announcement);
        if (ObjectUtils.isEmpty(announcement.getText()) || ObjectUtils.isEmpty(announcement.getTitle())) {
            return Result.failure(ResultCode.FAILURE_INSERT_ERROR, "标题或正文可不能留空白哟");
        }
        String token = insertService.insetAnnouncement(announcement, request);
        if (!ObjectUtils.isEmpty(token)) {
            if (token.equals("1401"))
                return Result.failure(ResultCode.FAILURE_INSERT_ERROR, "标题不能超过20个字符哦~");
            else
                return Result.success("发布成功");
        }

        return Result.failure(ResultCode.FAILURE_INSERT_ERROR);
    }
    @PostMapping("/index/member/insert")
    public Result<String> insertUser(UserEntity user, HttpServletRequest request) {
        System.out.println("前端参数:id=" + user );
        if (ObjectUtils.isEmpty(user.getUsername()) || ObjectUtils.isEmpty(user.getPassword()) || ObjectUtils.isEmpty(user.getStatus())) {
            return Result.failure(ResultCode.FAILURE_INSERT_ERROR, "所以填写信息不能为空哦");
        }
        String token = insertService.insertUser(user, request);
        if (!ObjectUtils.isEmpty(token)) {
            return Result.success("添加成功");
        }
        return Result.failure(ResultCode.FAILURE_INSERT_ERROR, "用户名已经存在");
    }
}
